using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using T.FAS.Runtime.Action.Entity;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Action.Dao
{
    public abstract class AbsDBInfoViewDao:IDBInfoViewDao
    {
        public abstract DataBaseInfoView GetDataBaseInfoViewByDataBaseCode(DataBaseInfo info);

        public  ITDatabase GetDatabaseByConfigkey(string configkey)
        {
            try
            {
                var db = DatabaseHelper.GetDatabaseByConnectionKey(configkey);
                if (db == null)
                    throw new FasException(ActionExecuteExceptionCode.ActionDBInfoHelperError, string.Format(CommonStringClassDao.AbsDBInfoViewDao_GetDatabaseByConfigkey,configkey));
                return db;
            }
            catch (Exception e)
            {
                throw new FasException(ActionExecuteExceptionCode.ActionDBInfoHelperError, string.Format(CommonStringClassDao.AbsDBInfoViewDao_GetDatabaseByConfigkey1,configkey), e);
            }
        }

        public List<DataBaseItemView> ConvertToItems(DataSet ds, DataBaseItemType itemType)
        {
            if (ds == null || ds.Tables == null || ds.Tables.Count < 0)
                return null;
            var list = new List<DataBaseItemView>();
            int index = 0;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var tableName = Convert.ToString(row["TableName"]);
                var item = new DataBaseItemView()
                {
                    ID = tableName,
                    Code = tableName,
                    Name = tableName,
                    Description = tableName,
                    DataBaseItemType = itemType,
                    SortOrder = index
                };
                if (!list.Exists(x => x.ID.Equals(item.ID, StringComparison.OrdinalIgnoreCase)))
                {
                    list.Add(item);
                    index++;
                }
            }
            return list;
        }

        public List<DataBaseItemView> ConvertToColumnItems(DataSet ds)
        {
            if (ds == null || ds.Tables == null || ds.Tables.Count < 0)
                return null;
            var list = new List<DataBaseItemView>();
            int index = 0;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var columnName = Convert.ToString(row["ColumnName"]);
                var dataType = Convert.ToString(row["DataType"]);
                var item = new DataBaseItemView()
                {
                    ID = columnName,
                    Code = columnName,
                    Name = columnName,
                    Description = columnName,
                    DataBaseItemType = DataBaseItemType.Column,
                    DataType = dataType,
                    SortOrder = index
                };
                if (!list.Exists(x => x.ID.Equals(item.ID, StringComparison.OrdinalIgnoreCase)))
                {
                    list.Add(item);
                    index++;
                }
            }
            return list;
        }
    }
}
